Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Commit 77c6157

Browse files
authored
Merge pull request #18 from l-jonas/de-hardcode-database-implementation
De-hardcode database implementation
2 parents a42ef00 + df72416 commit 77c6157

File tree

9 files changed

+39
-24
lines changed

9 files changed

+39
-24
lines changed

bep/src/main/kotlin/net/syncthing/java/bep/IndexBrowser.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class IndexBrowser internal constructor(private val indexRepository: IndexReposi
129129

130130
fun listFiles(path: String = currentPath): List<FileInfo> {
131131
logger.debug("doListFiles for path = '{}' BEGIN", path)
132-
val list = indexRepository.findNotDeletedFilesByFolderAndParent(folder, path)
132+
val list = ArrayList(indexRepository.findNotDeletedFilesByFolderAndParent(folder, path))
133133
logger.debug("doListFiles for path = '{}' : {} records loaded)", path, list.size)
134134
if (includeParentInList && (!PathUtils.isRoot(path) || allowParentInRoot)) {
135135
list.add(0, PARENT_FILE_INFO)

client-cli/build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apply plugin: 'application'
2+
mainClassName = 'net.syncthing.java.client.cli.Main'
3+
4+
dependencies {
5+
compile project(':syncthing-java')
6+
compile project(':repository')
7+
compile "commons-cli:commons-cli:1.4"
8+
}
9+
10+
run {
11+
if (project.hasProperty('args')) {
12+
args project.args.split('\\s+')
13+
}
14+
}

client/src/main/kotlin/net/syncthing/java/client/Main.kt client-cli/src/main/kotlin/net/syncthing/java/client/cli/Main.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
*/
14-
package net.syncthing.java.client
14+
package net.syncthing.java.client.cli
1515

1616
import net.syncthing.java.core.beans.DeviceId
1717
import net.syncthing.java.core.beans.DeviceInfo
1818
import net.syncthing.java.core.beans.FileInfo
1919
import net.syncthing.java.core.configuration.Configuration
20+
import net.syncthing.java.repository.repo.SqlRepository
21+
import net.syncthing.java.client.SyncthingClient
2022
import org.apache.commons.cli.*
2123
import org.apache.commons.io.FileUtils
2224
import org.slf4j.LoggerFactory
@@ -41,7 +43,9 @@ class Main(private val commandLine: CommandLine) {
4143
val configuration = if (cmd.hasOption("C")) Configuration(File(cmd.getOptionValue("C")))
4244
else Configuration()
4345

44-
SyncthingClient(configuration).use { syncthingClient ->
46+
val repository = SqlRepository(configuration.databaseFolder)
47+
48+
SyncthingClient(configuration, repository, repository).use { syncthingClient ->
4549
val main = Main(cmd)
4650
cmd.options.forEach { main.handleOption(it, configuration, syncthingClient) }
4751
}

client/build.gradle

-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
apply plugin: 'application'
2-
mainClassName = 'net.syncthing.java.client.Main'
3-
41
dependencies {
52
compile project(':core')
63
compile project(':bep')
7-
compile project(':repository')
84
compile project(':discovery')
9-
compile "commons-cli:commons-cli:1.4"
10-
}
11-
12-
run {
13-
if (project.hasProperty('args')) {
14-
args project.args.split('\\s+')
15-
}
165
}

client/src/main/kotlin/net/syncthing/java/client/SyncthingClient.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import net.syncthing.java.core.beans.DeviceAddress
2121
import net.syncthing.java.core.beans.DeviceId
2222
import net.syncthing.java.core.beans.DeviceInfo
2323
import net.syncthing.java.core.configuration.Configuration
24+
import net.syncthing.java.core.interfaces.IndexRepository
25+
import net.syncthing.java.core.interfaces.TempRepository
2426
import net.syncthing.java.core.security.KeystoreHandler
2527
import net.syncthing.java.core.utils.awaitTerminationSafe
2628
import net.syncthing.java.discovery.DiscoveryHandler
27-
import net.syncthing.java.repository.repo.SqlRepository
2829
import org.slf4j.LoggerFactory
2930
import java.io.Closeable
3031
import java.io.IOException
@@ -35,11 +36,14 @@ import java.util.concurrent.TimeUnit
3536
import java.util.concurrent.atomic.AtomicBoolean
3637
import kotlin.collections.ArrayList
3738

38-
class SyncthingClient(private val configuration: Configuration) : Closeable {
39+
class SyncthingClient(
40+
private val configuration: Configuration,
41+
private val repository: IndexRepository,
42+
private val tempRepository: TempRepository
43+
) : Closeable {
3944

4045
private val logger = LoggerFactory.getLogger(javaClass)
4146
val discoveryHandler: DiscoveryHandler
42-
private val sqlRepository = SqlRepository(configuration.databaseFolder)
4347
val indexHandler: IndexHandler
4448
private val connections = Collections.synchronizedSet(createConnectionsSet())
4549
private val connectByDeviceIdLocks = Collections.synchronizedMap(HashMap<DeviceId, Object>())
@@ -49,7 +53,7 @@ class SyncthingClient(private val configuration: Configuration) : Closeable {
4953
private fun createConnectionsSet() = TreeSet<ConnectionHandler>(compareBy { it.address.score })
5054

5155
init {
52-
indexHandler = IndexHandler(configuration, sqlRepository, sqlRepository)
56+
indexHandler = IndexHandler(configuration, repository, tempRepository)
5357
discoveryHandler = DiscoveryHandler(configuration)
5458
connectDevicesScheduler.scheduleAtFixedRate(this::updateIndexFromPeers, 0, 15, TimeUnit.SECONDS)
5559
}
@@ -219,7 +223,8 @@ class SyncthingClient(private val configuration: Configuration) : Closeable {
219223
// Create copy of list, because it will be modified by handleConnectionClosedEvent(), causing ConcurrentModificationException.
220224
ArrayList(connections).forEach{it.close()}
221225
indexHandler.close()
222-
sqlRepository.close()
226+
repository.close()
227+
tempRepository.close()
223228
assert(onConnectionChangedListeners.isEmpty())
224229
}
225230

core/src/main/kotlin/net/syncthing/java/core/interfaces/IndexRepository.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package net.syncthing.java.core.interfaces
1515

1616
import net.syncthing.java.core.beans.*
17+
import java.io.Closeable
1718
import java.util.*
1819

19-
interface IndexRepository {
20+
interface IndexRepository: Closeable {
2021

2122
fun setOnFolderStatsUpdatedListener(listener: ((IndexRepository.FolderStatsUpdatedEvent) -> Unit)?)
2223

@@ -36,7 +37,7 @@ interface IndexRepository {
3637

3738
fun updateFileInfo(fileInfo: FileInfo, fileBlocks: FileBlocks?)
3839

39-
fun findNotDeletedFilesByFolderAndParent(folder: String, parentPath: String): MutableList<FileInfo>
40+
fun findNotDeletedFilesByFolderAndParent(folder: String, parentPath: String): List<FileInfo>
4041

4142
fun clearIndex()
4243

core/src/main/kotlin/net/syncthing/java/core/interfaces/TempRepository.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
package net.syncthing.java.core.interfaces
1515

16-
interface TempRepository {
16+
import java.io.Closeable
17+
18+
interface TempRepository: Closeable {
1719

1820
fun pushTempData(data: ByteArray): String
1921

discovery/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mainClassName = 'net.syncthing.java.discovery.Main'
33

44
dependencies {
55
compile project(':core')
6-
compile project(':repository')
76
compile "commons-cli:commons-cli:1.4"
87
}
98

settings.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include ':bep'
66
include ':client'
77
include ':repository'
88
include ':discovery'
9+
include ':client-cli'
910

1011
// Also call this syncthing-java so it can be easily used from syncthing-lite.
11-
project(':client').name = 'syncthing-java'
12+
project(':client').name = 'syncthing-java'

0 commit comments

Comments
 (0)