-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzzz.java
More file actions
188 lines (132 loc) · 9.19 KB
/
zzz.java
File metadata and controls
188 lines (132 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
public class zzz {
}
// optional exampe given
String s = null;
Optional<String> optionalS = Optional.ofNullable(s);
if (optionalS.isPresent()) {
System.out.println("The string is: " + optionalS.get());
} else {
System.out.println("The string is null.");
}
// ========================================
Here is an example database design for a message service in Python Django:
Users table:
id: primary key
username: string
email: string
password: string
Messages table:
id: primary key
sender_id: foreign key to Users table
receiver_id: foreign key to Users table
text: string
timestamp: datetime
Conversations table:
id: primary key
name: string
timestamp: datetime
ConversationMembers table:
id: primary key
conversation_id: foreign key to Conversations table
user_id: foreign key to Users table
ConversationMessages table:
id: primary key
conversation_id: foreign key to Conversations table
message_id: foreign key to Messages table
===============================================
Sure, here''s an example of a messaging schema for a social media platform:
User table:
id: Primary key (integer)
name: String
email: String
password: String
Conversation table:
id: Primary key (integer)
subject: String
Message table:
id: Primary key (integer)
content: String
sender_id: Foreign key (integer) referencing User(id)
conversation_id: Foreign key (integer) referencing Conversation(id)
created_at: Timestamp
ConversationParticipant table:
user_id: Foreign key (integer) referencing User(id)
conversation_id: Foreign key (integer) referencing Conversation(id)
In this schema, we have four tables: User, Conversation, Message, and ConversationParticipant. The User table contains information about each user, with an id column as the primary key. The Conversation table contains information about each conversation, with an id column as the primary key and a subject column for the conversation topic. The Message table contains information about each message, with an id column as the primary key, a content column for the message text, a sender_id column as a foreign key referencing the User table, a conversation_id column as a foreign key referencing the Conversation table, and a created_at column for the timestamp when the message was sent. The ConversationParticipant table contains information about each participant in a conversation, with a user_id column as a foreign key referencing the User table and a conversation_id column as a foreign key referencing the Conversation table.
By using these tables and their relationships, we can establish conversations between users and messages sent between them. The ConversationParticipant table helps to keep track of the users participating in a conversation, while the Message table stores information about each message sent, including the sender and the conversation it belongs to.
I hope this example helps!
================================================================
The @Bean annotation has a scope attribute that can be used to define the scope of the bean. The default scope for a bean is "singleton", which means that Spring will create a single instance of the bean and reuse it throughout the application context.
Here are the different scopes that can be used with the @Bean annotation in Spring Boot:
Singleton (default): A single instance of the bean will be created and shared by all callers.
Prototype: A new instance of the bean will be created each time it is requested from the application context.
Request: A new instance of the bean will be created for each HTTP request.
Session: A new instance of the bean will be created for each HTTP session.
Global Session: Only used with Portlet context. A single instance of the bean will be created for each global Portlet session.
@Bean
@Scope("prototype")
public MyBean myBean() {
return new MyBean();
}
In this example, the MyBean instance will have a prototype scope, which means that a new instance will be created each time the bean is requested from the application context.
Note that the scope of a bean can also be configured in the @Configuration class by using the @Scope annotation on the @Bean method. Alternatively, you can use the @Scope annotation on the class itself to define the scope for all beans of that class.
============================================================================
here's an example of a singleton class that is thread-safe and not cloneable:
public class MySingleton implements Cloneable {
private static volatile MySingleton instance;
private MySingleton() {
// Private constructor to prevent instantiation
}
public static MySingleton getInstance() {
if (instance == null) {
synchronized (MySingleton.class) {
if (instance == null) {
instance = new MySingleton();
}
}
}
return instance;
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
============================================================================
an implementation of a thread-safe singleton class using an enum in Java:
public enum MySingleton {
INSTANCE;
public void doSomething() {
// implementation of method
}
}
This implementation makes use of the fact that Java's enum type is inherently thread-safe and provides a concise and easy-to-understand way to create a singleton instance.
The MySingleton class has a single instance, INSTANCE, which can be accessed from anywhere in the code using the syntax MySingleton.INSTANCE. This instance is created automatically by the JVM when the enum is loaded, and is guaranteed to be thread-safe and immutable.
The doSomething() method can be used to perform any operations that the singleton is intended to perform. Any code that needs to use the singleton can call this method on the INSTANCE instance.
Using an enum to create a singleton is considered to be one of the best practices in Java for implementing a thread-safe singleton, as it is simple, concise, and provides built-in thread safety and serialization support.
=================================================================================
NoSQL DBs are mostly based on eventual consistency or weak consistency models, which basically means writes many not be immediately available. This allow faster writes, as replication to other nodes can be deferred. It is possible to configure some RDBMS in async replication as well, but that will not guarantee ACID. The RDBMS with ACID can be scaled for read very easily but same is not true for write.
LSM trees are by design better suited for write heavy workload. BTrees based RDBMS are more suited for read and will give predictable decent performance of writes. LSM Tree based NoSQLs writes many have spikes but on average it will perform better. LSMTree merge random writes to sequential write which will again perform better with hardware.
Schema changes in RDBMS are though battles, for NoSQLs not so much. If you go very large with RDBMS, applying these changes would take time. For NoSQLs it can be handled in application code.
Due to above reasons NoSQL DBs can be scaled to PetaBytes scales, while RDBMS may find it hard.
Lastly in tech industry perception of "new is always better" will always be there.'
=================================================================================
JAVA 8 streams API Examples
// Accumulate names into a TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = things.stream().map(Object::toString).collect(Collectors.joining(", "));
// Compute sum of salaries of employee
int total = employees.stream().collect(Collectors.summingInt(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDep = employees.stream().collect(Collectors.groupingBy(Employee::getDepartment));
// Compute sum of salaries by department
Map<Department, Integer> totalByDept = employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.summingInt(Employee::getSalary)));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing = students.stream().collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD))
// The following will accumulate strings into an ArrayList:
List<String> asList = stringStream.collect(Collectors.toList());
// The following will classify Person objects by city:
Map<String, List<Person>> peopleByCity = personStream.collect(Collectors.groupingBy(Person::getCity));
// The following will classify Person objects by state and city, cascading two Collectors together:
Map<String, Map<String, List<Person>>> peopleByStateAndCity = personStream.collect(Collectors.groupingBy(Person::getState,Collectors.groupingBy(Person::getCity)));